package modularity.passiveDrift;

import java.util.ArrayList;

public class ModularGenomeFactory implements GenomeFactory<ModularGenome> {

	@Override
	public ListedPopulation generateInitialPopulation(int populationSize, int inputNodes, int outputNodes) {
		ListedPopulation pop = new ListedPopulation(populationSize);

		ArrayList<Integer> inputs = new ArrayList<Integer>(inputNodes);
		for(int i = 1; i <= inputNodes; i++)
			inputs.add(i);

		ArrayList<Integer> outputs = new ArrayList<Integer>(outputNodes);
		for(int i = inputNodes + 1; i <= inputNodes + outputNodes; i++)
			outputs.add(i);		

		for(int i = 0; i < populationSize; i++){
			ModularGenome genome = generateGenome(inputs, outputs);
			setName(genome, String.valueOf(i+1));
			pop.add(genome);
		}
		return pop;
	}

	@Override
	public ModularGenome generateGenome(ArrayList<Integer> inputNodes, ArrayList<Integer> outputNodes) {
		int numberOfModules = inputNodes.size() * outputNodes.size();
		BasicGenome[] modules = new BasicGenome[numberOfModules];
		int index = 0;
		for(int input : inputNodes){
			for(int output: outputNodes){
				modules[index++] = new BasicGenome(input, output);
			}
		}
		return new ModularGenome(modules);
	}

	@Override
	public void setName(ModularGenome element, String name) {
		element.setName(name);
	}

}
